package com.awenhui.commonweb.validation.validator;

import com.awenhui.commonweb.validation.annotation.Account;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 账号验证逻辑
 *
 * @author zxiaozhou
 * @date 2019-06-18 10:44
 * @since JDK1.8
 */
public class AccountValidator implements ConstraintValidator<Account, String> {
    @Override
    public void initialize(Account constraintAnnotation) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isBlank(value)) {
            return true;
        }
        String regexp = "^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z\\d]{6,17}$";
        return value.matches(regexp);
    }
}
